import { defineStore } from "pinia";
import type { cateGoryListType } from "@/api/category";
import {
  reqcateGory1List,
  reqcateGory2List,
  reqcateGory3List,
} from "@/api/category";

export interface cateGoryStateType {
  cateGory1List: cateGoryListType;
  cateGory2List: cateGoryListType;
  cateGory3List: cateGoryListType;
  category1Id: null | number;
  category2Id: null | number;
  category3Id: null | number;
}
const useCateGoryStore = defineStore("category", {
  state: (): cateGoryStateType => {
    return {
      cateGory1List: [],
      cateGory2List: [],
      cateGory3List: [],
      category1Id: null,
      category2Id: null,
      category3Id: null,
    };
  },
  actions: {
    // 请求一级分类列表
    async getCateGory1List() {
      try {
        const re = await reqcateGory1List();
        this.cateGory1List = re;
      } catch (e) {
        return Promise.reject(e);
      }
    },
    async getCateGory2List() {
      try {
        const re = await reqcateGory2List(this.category1Id as number);
        this.cateGory2List = re;
      } catch (e) {
        return Promise.reject(e);
      }
    },
    async getCateGory3List() {
      try {
        const re = await reqcateGory3List(this.category2Id as number);
        this.cateGory3List = re;
      } catch (e) {
        return Promise.reject(e);
      }
    },
  },
});

export default useCateGoryStore
